//
//  NovelDetailsTableViewCell.m
//  A_Program
//
//  Created by dlios on 15-5-22.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "NovelDetailsTableViewCell.h"
#import "NovelDetailsModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "MyConllectButton.h"
#import "NovelDatabase.h"
#import "NovelModel.h"

@implementation NovelDetailsTableViewCell
- (void)dealloc
{
    [self.myView release];
    [self.detailsImageView release];
    [self.detailsImageView release];
    [self.detailsTitleLable release];
    [self.detailsFristImage release];
    [self.detailsFristLable release];
    [self.detailsSecondImage release];
    [self.detailsSecondLable release];
    [self.detailsThirdImage release];
    [self.detailsThirdLable release];
    [self.detailsColletButton release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:0.9]];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/6 - 10)];
        [self addSubview:self.myView];
        [self.myView setBackgroundColor:[UIColor whiteColor]];
        [self createDetailsCell];
        [self.myView release];
        
    }
    return self;
}
- (void)createDetailsCell{
    self.detailsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/3.5, [UIScreen mainScreen].bounds.size.height/6 - 10)];
    [self.myView addSubview:self.detailsImageView];
    [self.detailsImageView release];
    
    self.detailsTitle = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3.5 + 10, 15, 20, 20)];
    self.detailsTitle.text = @"辑";
    self.detailsTitle.textAlignment = NSTextAlignmentCenter;
    self.detailsTitle.font = [UIFont systemFontOfSize:15];
    self.detailsTitle.textColor = [UIColor orangeColor];
    self.detailsTitle.layer.borderWidth = 1;
    self.detailsTitle.layer.borderColor = [UIColor orangeColor].CGColor;
    self.detailsTitle.layer.masksToBounds = YES;
    self.detailsTitle.layer.cornerRadius = 10;
    [self.myView addSubview:self.detailsTitle];
    [self.detailsTitle release];
    
    self.detailsTitleLable = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3.5 + 30, 10, [UIScreen mainScreen].bounds.size.width/3.5*2.5-10, 30)];
    [self.myView addSubview:self.detailsTitleLable];
//    self.detailsTitleLable.textColor = [UIColor blueColor];
    [self.detailsTitleLable release];
  //第一个
    self.detailsFristImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3.5 + 10, [UIScreen mainScreen].bounds.size.height*0.09, 20, 20)];
    [self.detailsFristImage setImage:[UIImage imageNamed:@"playcount1.png"]];
    [self.myView addSubview:self.detailsFristImage];
    [self.detailsFristImage release];
    self.detailsFristLable = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3.5 + 30, [UIScreen mainScreen].bounds.size.height*0.09, 60, 20)];
//    self.detailsFristLable.textColor = [UIColor colorWithRed:0 green:0.3 blue:1 alpha:1];
    self.detailsFristLable.font = [UIFont systemFontOfSize:12];
    [self.myView addSubview:self.detailsFristLable];
    [self.detailsFristLable release];
    //第二个
    self.detailsSecondImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3.5 + 90, [UIScreen mainScreen].bounds.size.height*0.09, 20, 20)];
    [self.detailsSecondImage setImage:[UIImage imageNamed:@"soundcount1.png"]];
    [self.myView addSubview:self.detailsSecondImage];
    [self.detailsSecondImage release];
    self.detailsSecondLable = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3.5 + 110, [UIScreen mainScreen].bounds.size.height*0.09, 60, 20)];
//    self.detailsSecondLable.textColor = [UIColor colorWithRed:0 green:0.3 blue:1 alpha:1];
    self.detailsSecondLable.font = [UIFont systemFontOfSize:12];
    [self.myView addSubview:self.detailsSecondLable];
    [self.detailsSecondLable release];
    
    //button
    self.detailsColletButton = [[MyConllectButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/12*10,[UIScreen mainScreen].bounds.size.height*0.1, 50, 30)];
    [self addSubview:self.detailsColletButton];
    
    self.detailsColletButton.layer.borderColor = [UIColor orangeColor].CGColor;
    self.detailsColletButton.layer.borderWidth = 1.0f;
    self.detailsColletButton.layer.cornerRadius = 5.0f;
    self.detailsColletButton.layer.masksToBounds = YES;
    self.detailsColletButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.detailsColletButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.myView addSubview:self.detailsColletButton];
    [self.detailsColletButton addTarget:self action:@selector(detailsColletButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailsColletButton release];
    
}

//按钮点击事件
- (void)detailsColletButtonAction:(id)sender{

#pragma mark-
#pragma mark 判断是否已收藏
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSInteger myAlbumId = [self.detailsColletButton.collectId integerValue];
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:myAlbumId loginName:[myLoginName objectForKey:@"loginName"]];
    if ([array count] == 0 && [self.detailsColletButton.titleLabel.text isEqualToString:@"收藏"]) {
        
        [self.collectDelegate createCollectAction:self.detailsColletButton.collectId];
    
        if (![[myLoginName objectForKey:@"loginName"] length] == 0 &&[[myLoginName objectForKey:@"state"]isEqualToString:@"登录"]) {
            [self.detailsColletButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }else{
            NSLog(@"未登录或则没有用户");
            
            
        }
        
        
    }else if([self.detailsColletButton.titleLabel.text isEqualToString:@"已收藏"]){
        
        NovelModel *myid = [[NovelModel alloc]init];
        for (NovelModel *model in array) {
            myid = model;
        }
        [NovelDatabase deleteWithModel:myid];
        [self.detailsColletButton setTitle:@"收藏" forState:UIControlStateNormal];

    }else{
        NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:[self.detailsColletButton.collectId integerValue] loginName:[myLoginName objectForKey:@"loginName"]];
        if ( ![array count] == 0) {
             [self.detailsColletButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }
    }
}
//视图导入数据
- (void)createDetailsTableViewCell:(NovelDetailsModel *)detailsModel{
    [self.detailsImageView sd_setImageWithURL:[NSURL URLWithString:detailsModel.albumCoverUrl290] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    [self.detailsTitleLable setText:detailsModel.title];
    NSString *count = [NSString stringWithFormat:@"%ld万",detailsModel.playsCounts/10000];
    [self.detailsFristLable setText:count];
    NSString *tcounts = [NSString stringWithFormat:@"%ld",detailsModel.tracksCounts];
    [self.detailsSecondLable setText:tcounts];
    //按钮的id
    self.detailsColletButton.collectId = detailsModel.myId;
    //功能
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:[detailsModel.myId integerValue] loginName:[myLoginName objectForKey:@"loginName"]];
    if (array.count == 0) {
        [self.detailsColletButton setTitle:@"收藏" forState:UIControlStateNormal];
        
    }else{
        [self.detailsColletButton setTitle:@"已收藏" forState:UIControlStateNormal];
    }

    
}
- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
